// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.designer.ui.view;

import org.dengues.commons.CommonsPlugin;
import org.dengues.commons.ui.swt.colorstyledtext.ColorManager;
import org.dengues.commons.ui.swt.colorstyledtext.ColorStyledText;
import org.dengues.commons.ui.swt.colorstyledtext.EModeLanguage;
import org.dengues.designer.ui.database.editors.DBFileEditorInput;
import org.dengues.designer.ui.editors.DatabaseMultiEditor;
import org.dengues.model.database.EMFDataType;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.part.ViewPart;

public class QueryViewer extends ViewPart implements ISelectionListener {

    private ColorStyledText colorText;

    public QueryViewer() {
    }

    @Override
    public void createPartControl(Composite parent) {
        parent.setLayout(new FillLayout());
        ColorManager colorManager = new ColorManager(CommonsPlugin.getDefault().getPreferenceStore());
        colorText = new ColorStyledText(parent, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL, colorManager, EModeLanguage.SQL
                .getLanguageMode());
        Font font = new Font(parent.getDisplay(), "courier", 10, SWT.NONE); //$NON-NLS-1$
        colorText.setFont(font);
        GridData gd = new GridData(GridData.FILL_BOTH);
        colorText.setLayoutData(gd);
        getSite().getWorkbenchWindow().getSelectionService().addSelectionListener(this);
    }

    @Override
    public void setFocus() {
        if (colorText != null) {
            colorText.setFocus();
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.ISelectionListener#selectionChanged(org.eclipse.ui.IWorkbenchPart,
     * org.eclipse.jface.viewers.ISelection)
     */
    public void selectionChanged(IWorkbenchPart part, ISelection selection) {
        if ((part instanceof DatabaseMultiEditor) && (selection instanceof IStructuredSelection)) {
            DatabaseMultiEditor editor = (DatabaseMultiEditor) part;
            DBFileEditorInput editorInput = (DBFileEditorInput) editor.getEditorInput();
            if (editorInput.getDbDiagram().getEmfType().getValue() == EMFDataType.QUERY) {
                String sqlStatement = editorInput.getSqlStatement();
                colorText.setText(sqlStatement);
            }
        }
    }

}
